﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace BackToCOS
{
   public class FileHelper
    {
        public List<string> GetAllFilesByDirectoryPath(string path)
        {
            List<string> list = new List<string>();
            var dirList = GetAllFiles(path,ref list);
           
            return list;
        }
        /// <summary>
        /// 获取目录下文件（一级）
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <returns></returns>
        public List<string> GetFiles(string directoryPath)
        {
            List<string> list = new List<string>();
            var rootDir = new DirectoryInfo(directoryPath);
            foreach (FileInfo dir in rootDir.GetFiles())
            {
                list.Add(dir.FullName);
            }
            return list;
        }
        /// <summary>
        /// 获取所有子目录
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <returns></returns>
        public List<string> GetAllDirectorys(string directoryPath)
        {
            List<string> list = new List<string>();
            var rootDir = new DirectoryInfo(directoryPath);
            foreach (DirectoryInfo dir in rootDir.GetDirectories())
            {
                list.Add(dir.FullName);
                GetAllDirectorys(dir.FullName);
            }
            return list;
        }
        /// <summary>
        /// 获取所有文件
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <returns></returns>
        private List<string> GetAllFiles(string directoryPath,ref List<string> fileList)
        {
            List<string> list = new List<string>();
            var rootDir = new DirectoryInfo(directoryPath);
            fileList.AddRange(rootDir.GetFiles().Select(x=>x.FullName));
            foreach (DirectoryInfo dir in rootDir.GetDirectories())
            {
                list.Add(dir.FullName);
                GetAllFiles(dir.FullName,ref fileList);
            }
            return list;
        }
    }
}
